const is = (tokens) => {
  return tokens.peek().type === 'keyword' && tokens.peek().value === 'set'
}

const parse = (tokens) => {
  const node = {
    type: 'AssignmentExpression',
    operator: '',
    left: {
      type: null,
      name: null
    },
    right: {
      type: null,
      value: null
    }
  }

  tokens.next()

  if (tokens.peek().type !== 'identifier') {
    tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
  }

  node.left = {
    type: 'Identifier',
    name: tokens.peek().value
  }

  tokens.next()

  if (tokens.peek().type === 'operator') {

    if (tokens.peek().type === 'operator' && tokens.peek().value === '=') {
      node.operator = tokens.peek().value
      tokens.next()

      if (tokens.peek().type !== 'number' && tokens.peek().type !== 'literal' && tokens.peek().type !== 'identifier') {
        tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
      }

      if (tokens.peek().type === 'number' || tokens.peek().type === 'literal') {
        node.right = {
          type: 'Literal',
          value: tokens.peek().value
        }
      } else if (tokens.peek().type === 'identifier') {
        node.right = {
          type: 'Identifier',
          name: tokens.peek().value
        }
      }

      tokens.next()
    } else if (tokens.peek().type === 'operator' && (tokens.peek().value === '++' || tokens.peek().value === '--' || tokens.peek().value === '+=' || tokens.peek().value === '-=' || tokens.peek().value === '*=' || tokens.peek().value === '/=' || tokens.peek().value === '%=')) {
      if (tokens.peek().type === 'operator' && (tokens.peek().value === '++' || tokens.peek().value === '--')) {
        node.operator = tokens.peek().value === '++' ? '+=' : '-='
        node.right = {
          type: 'Literal',
          value: 1
        }

        tokens.next()
      } else {
        node.operator = tokens.peek().value
        tokens.next()

        if (tokens.peek().type !== 'number' && tokens.peek().type !== 'identifier') {
          tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
        }

        if (tokens.peek().type === 'number') {
          node.right = {
            type: 'Literal',
            value: tokens.peek().value
          }
        } else if (tokens.peek().type === 'identifier') {
          node.right = {
            type: 'Identifier',
            name: tokens.peek().value
          }
        }

        tokens.next()
      }
    } else {
      tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
    }

  } else {
    tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
  }

  return node
}

export default {
  is,
  parse
}